package com.qfedu.edu.seckill.utils;

import com.alibaba.fastjson.JSON;
import com.qfedu.edu.seckill.result.R;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 这个是Http响应的这个工具类
 */
public class HttpResponseUtils {

    /**
     * 向客户端回应消息
     * @param resp
     * @param r
     */
    public static void sendResponse(HttpServletResponse resp, R r){
        String s = JSON.toJSONString(r);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            writer.write(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            writer.flush();
            writer.close();
        }


    }


}
